//
//  DXMLLeagueObject.m
//  DerbyStatter
//
//  Created by glenn andreas on 1/6/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DXMLLeagueObject.h"
#import "DXMLTeamObject.h"

@implementation DXMLLeagueObject
- (DXMLTeamObject *) teamWithName: (NSString *)name create: (BOOL) create
{
    DXMLTeamObject *retval = [self firstChildWithElementName:  name];
    if (!retval) {
        if (create) {
            retval = [[DXMLTeamObject alloc] init];
//            retval.league = self;
            retval.name = name;
            [self addChild:retval];
        }
    }
    return retval;
}
- (void) addTeam: (DXMLTeamObject *) team
{
    if (team.league != self) {
        [team.league removeChild:team];
    }
//    team.league = self;
    [self addChild: team];
}
- (void) addChild:(DXMLObject *)child
{
    if ([child isKindOfClass:[DXMLTeamObject class]]) {
        if (!self.teams) {
            self.teams = [NSMutableArray array];
        }
        [(NSMutableArray *)self.teams addObject: child];
    }
    [super addChild:child];
}
- (void) removeChild:(DXMLObject *)child
{
    if ([(NSMutableArray *)self.teams containsObject:child]) {
        [(NSMutableArray *)self.teams removeObject:child];
    }
    [super removeChild:child];
}
@end
